<!-- 简化版任务详情页面 -->
<template>
  <scroll-view 
    scroll-y="true" 
    :refresher-enabled="true"
    :refresher-triggered="refreshing"
    @refresherrefresh="onRefresh"
    class="scroll-container"
  >
    <!-- 加载状态 -->
    <view v-if="loading" class="loading">
      <text>加载中...</text>
    </view>
    
    <!-- 错误状态 -->
    <view v-else-if="error" class="error">
      <text>{{ error }}</text>
    </view>
    
    <!-- 主要内容 -->
    <view v-else>
      <!-- 任务头部信息 -->
      <view class="task-header">
        <image 
          :src="getAvatarUrl(taskData?.userAvatar)" 
          mode="aspectFill" 
          class="user-avatar" 
          @error="handleTaskAvatarError" 
        />
        <view class="header-main">
          <view class="header-title-row">
            <text class="task-title">{{ taskData?.title || '加载中...' }}</text>
            <view class="status-tag">
              <text>{{ getStatusText(taskData?.status) }}</text>
            </view>
          </view>
          <view class="task-publisher">
            发布者：{{ taskData?.userName || '未知用户' }} ({{ taskData?.orgName || '未知企业' }})
          </view>
          <view class="task-time">发布时间：{{ taskData?.publishTime || '未知' }}</view>
          <view class="task-deadline">截止日期：{{ taskData?.deadline || '未知' }}</view>
        </view>
      </view>
      
      <!-- 任务详情 -->
      <view class="task-body">
        <view class="task-info-row">
          <text class="task-info-label">任务描述:</text>
          <text class="task-info-value">{{ taskData?.description || '暂无描述' }}</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">悬赏金额:</text>
          <text class="task-info-value">{{ formatNumber(taskData?.reward) }} 元</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">当前支付:</text>
          <text class="task-info-value">{{ formatNumber(taskData?.currentPaid) }} 元</text>
        </view>
        <view class="task-info-row">
          <text class="task-info-label">任务状态:</text>
          <text class="task-info-value">{{ getStatusText(taskData?.status) }}</text>
        </view>
        <view v-if="taskData?.challengerId && (taskData?.status === '2' || taskData?.status === '3')" class="task-info-row">
          <text class="task-info-label">挑战者:</text>
          <text class="task-info-value">UID {{ formatNumber(taskData?.challengerId) }}</text>
        </view>
      </view>

      <!-- 调试信息 -->
      <view class="debug-info">
        <text class="debug-title">调试信息:</text>
        <text class="debug-item">taskId: {{ taskId }}</text>
        <text class="debug-item">loading: {{ loading }}</text>
        <text class="debug-item">error: {{ error }}</text>
        <text class="debug-item">taskData: {{ JSON.stringify(taskData) }}</text>
      </view>
    </view>
  </scroll-view>
</template>

<script>
import { NETWORK_CONFIG } from '@/config/network.js'
export default {
  data() {
    return {
      taskId: null,
      taskData: null,
      loading: false,
      error: '',
      refreshing: false
    }
  },
  
  onLoad(options) {
    console.log('页面加载，参数:', options)
    if (options.taskId) {
      this.taskId = options.taskId
      this.loadPageData()
    } else {
      this.error = '缺少任务ID参数'
    }
  },
  
  onShow() {
    console.log('页面显示')
    if (this.taskId) {
      this.refreshPageData()
    }
  },
  
  methods: {
    async loadPageData() {
      console.log('开始加载页面数据，taskId:', this.taskId)
      this.loading = true
      this.error = ''
      
      try {
        await this.fetchTaskDetail(this.taskId)
      } catch (err) {
        console.error('加载页面数据失败:', err)
        this.error = err.message || '加载失败'
      } finally {
        this.loading = false
      }
    },
    
    async refreshPageData() {
      console.log('刷新页面数据')
      this.refreshing = true
      try {
        await this.fetchTaskDetail(this.taskId)
      } catch (err) {
        console.error('刷新页面数据失败:', err)
      } finally {
        this.refreshing = false
      }
    },
    
    async fetchTaskDetail(taskId) {
      console.log('获取任务详情，taskId:', taskId)
      
      if (!taskId) {
        throw new Error('任务ID不能为空')
      }
      
      try {
        const token = uni.getStorageSync('accessToken')
        if (!token) {
          throw new Error('请先登录')
        }
        
        console.log('发送请求到:', `${NETWORK_CONFIG.API_BASE_URL}/task/${taskId}`)
        
        const response = await uni.request({
          url: `${NETWORK_CONFIG.API_BASE_URL}/task/${taskId}`,
          method: 'GET',
          header: {
            'Access-Token': `Bearer ${token}`
          }
        })
        
        console.log('任务详情响应:', response)
        
        if (response.data && response.data.success) {
          this.taskData = response.data.data
          console.log('任务数据设置成功:', this.taskData)
        } else {
          throw new Error(response.data?.message || '获取任务详情失败')
        }
      } catch (err) {
        console.error('获取任务详情失败:', err)
        throw err
      }
    },
    
    onRefresh() {
      this.refreshPageData()
    },
    
    formatNumber(num) {
      if (!num) return '0'
      return Number(num).toLocaleString()
    },
    
    getStatusText(status) {
      const statusMap = {
        '0': '待接取',
        '1': '进行中',
        '2': '已完成',
        '3': '已取消',
        '4': '已过期'
      }
      return statusMap[status] || '未知状态'
    },
    
    getAvatarUrl(avatarUrl) {
      if (!avatarUrl) return '/static/images/default-avatar.png'
      if (avatarUrl.startsWith('http')) return avatarUrl
      return `${NETWORK_CONFIG.API_BASE_URL}${avatarUrl}`
    },
    
    handleTaskAvatarError() {
      console.log('任务头像加载失败')
    }
  }
}
</script>

<style scoped>
.scroll-container {
  height: 100vh;
  background: #f5f5f5;
}

.loading,
.error {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200rpx;
  font-size: 28rpx;
}

.loading {
  color: #666;
}

.error {
  color: #f44336;
}

.task-header {
  display: flex;
  padding: 20rpx;
  background: #fff;
  border-radius: 16rpx;
  margin: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.user-avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  margin-right: 20rpx;
  flex-shrink: 0;
}

.header-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.header-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10rpx;
}

.task-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
  margin-right: 20rpx;
}

.status-tag {
  background: #4CAF50;
  color: white;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
}

.task-publisher,
.task-time,
.task-deadline {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.task-deadline {
  color: #ff6b6b;
  font-weight: 500;
}

.task-body {
  background: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.task-info-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.task-info-row:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.task-info-label {
  font-size: 28rpx;
  color: #666;
  font-weight: 500;
  min-width: 160rpx;
}

.task-info-value {
  font-size: 28rpx;
  color: #333;
  flex: 1;
  text-align: right;
  line-height: 1.4;
}

.debug-info {
  margin: 20rpx;
  padding: 20rpx;
  background: #f8f9fa;
  border-radius: 12rpx;
  border: 2rpx dashed #dee2e6;
}

.debug-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 15rpx;
  display: block;
}

.debug-item {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
  display: block;
  word-break: break-all;
}
</style>
